/**
 * Created by Richter on 2018-11-07.
 * Modified by Richter on 2018-11-07.
 */

var Main = {
    getContextPath: function () {
        var pathName = document.location.pathname;
        var index = pathName.substr(1).indexOf("/");
        var result = pathName.substr(0, index + 1);
        return result;
    },
    requestOauthToken: function (username, password, onSuccess, onError) {
        $.ajax({
            url: Main.getContextPath() + '/oauth/token',
            datatype: 'json',
            type: 'post',
            headers: {'Authorization': 'Basic YnJvd3Nlcjo='},
            data: {
                scope: 'ui',
                username: username,
                password: password,
                grant_type: 'password'
            },
            success: function (data) {
                localStorage.setItem('token', data.access_token);
                if (onSuccess && typeof onSuccess == 'function')
                    onSuccess(data.access_token);
            }, error: function (data) {
                Main.removeOauthTokenFromStorage();
                if (onError && typeof onError == 'function')
                    onError();
            }
        });
    },
    getOauthTokenFromStorage: function () {
        return localStorage.getItem('token');
    },
    removeOauthTokenFromStorage: function () {
        return localStorage.removeItem('token');
    },
    getCurrentAccount: function (onSeccess, onError) {
        var token = Main.getOauthTokenFromStorage();
        if (token) {
            return $.ajax({
                url: Main.getContextPath() + '/auth/current',
                datatype: 'json',
                type: 'get',
                headers: {'Authorization': 'Bearer ' + token},
                statusCode: {
                    401: function () {
                        Main.removeOauthTokenFromStorage();
                        Main.showLogin();
                    }
                },
                success: onSuccess,
                error: onError
            });
        }
    },
    showLogin: function () {
        setTimeout(window.open(Main.getContextPath() + '/index', '_self'), 0)
    },
}

var Api = {
    defaultOptions: {
        datatype: 'json',
        statusCode: {
            401: function () {
                Main.removeOauthTokenFromStorage();
                Main.showLogin();
            }
        }
    },
    ajax: function (settings) {
        var token = Main.getOauthTokenFromStorage();
        var options = {
            headers: {'Authorization': 'Bearer ' + token}
        };
        if (token) {
            return $.ajax($.extend({}, this.defaultOptions, options, settings));
        } else {
            Main.showLogin();
        }
    },
    get: function (settings) {
        var options = {
            type: 'get'
        };
        this.ajax(($.extend({}, options, settings)));
    },
    post: function (settings) {
        var options = {
            type: 'post'
        };
        this.ajax(($.extend({}, options, settings)));
    },
    put: function (settings) {
        var options = {
            type: 'put'
        };
        this.ajax(($.extend({}, options, settings)));
    },
    delete: function (settings) {
        var options = {
            type: 'delete'
        };
        this.ajax(($.extend({}, options, settings)));
    },
    putJson: function (settings) {
        var options = {
            type: 'put',
            contentType: "application/json"
        };
        this.put(($.extend({}, options, settings)));
    }
}

$.fn.extend({
    /**
     * 表单form转json对象
     */
    serializeObject: function () {
        var serializeObj = {};
        var array = this.serializeArray();
        $(array).each(function () {
            if (serializeObj[this.name]) {
                if ($.isArray(serializeObj[this.name])) {
                    serializeObj[this.name].push(this.value);
                } else {
                    serializeObj[this.name] = [serializeObj[this.name], this.value];
                }
            } else {
                serializeObj[this.name] = this.value;
            }
        });
        return serializeObj;
    }
});

jQuery(document).ready(function () {

    /**
     * modal配置
     */
    $(".modal").each(function (index) {
        $(this).on('shown.bs.modal', function (e) {
            var open = $(this).attr('data-easein');
            if (open == 'shake') {
                $('.modal-dialog').velocity('callout.' + open);
            } else if (open == 'pulse') {
                $('.modal-dialog').velocity('callout.' + open);
            } else if (open == 'tada') {
                $('.modal-dialog').velocity('callout.' + open);
            } else if (open == 'flash') {
                $('.modal-dialog').velocity('callout.' + open);
            } else if (open == 'bounce') {
                $('.modal-dialog').velocity('callout.' + open);
            } else if (open == 'swing') {
                $('.modal-dialog').velocity('callout.' + open);
            } else {
                $('.modal-dialog').velocity('transition.' + open);
            }
        });
    });

    /**
     * toastr配置
     */
    toastr.options = {
        positionClass: 'toast-center',
        timeOut: 2100,
    };
});